//Mantenimiento
function removeFilter(Object){
	while (Object.id!='divCopia'){Object = Object.parentNode;}
	Object.parentNode.removeChild(Object);
	//alert (Object.innerHTML);
}
function showFilter(Object, value){
	while (Object.id!='divContainer'){Object = Object.parentNode;}
	var spanShow =   getElementById('showFilter',Object);
	var spanHidd =   getElementById('hiddenFilter',Object);
	var divFilter =   getElementById('divSearch',Object);
	if (value){
		spanShow.style.display="none";
		spanHidd.style.display="";
		divFilter.style.display="";
	}
	else{
		spanShow.style.display="";
		spanHidd.style.display="none";
		divFilter.style.display="none";
	}
}
function _add_Find(Object){
	while (Object.id!='divSearch'){Object = Object.parentNode;}
	//divSearch
	var divOriginal =   getElementById('divOriginal',Object);
	var spanBtn =   getElementById('btns',Object);
	var spanRemove = document.createElement('span');
	var btnRemove = document.createElement('button');
	var imgRemove = document.createElement('img');
	var select = document.createElement('select');
	var op1 = document.createElement('option');
	var op2 = document.createElement('option');
	spanRemove.innerHTML="<button onclick='removeFilter(this)'><img src='"+_PATH+"images/delete.gif'></img></button>";
	//btnRemove.appendChild(imgRemove);
	op1.value="and";
	op2.value="or";
	op1.innerHTML="Y";
	op2.innerHTML="O";
	select.appendChild(op1);
	select.appendChild(op2);
	var divNuevo = divOriginal.cloneNode(true);
	divOriginal.id='divCopia';
	divOriginal.appendChild(select);
	divOriginal.appendChild(spanRemove);
	//Object.insertBefore(btnRemove, spanBtn); 
	Object.insertBefore(divNuevo, spanBtn); 
	//divNuevo.insertBefore(btnRemove, divNuevo.lastChild);
	//Object.innerHTML="";
}
//Obtener Cadena de Restriccions
function _get_Find(Object){
	var rpta="";
	while (Object.id!='divBody'){Object = Object.parentNode;}
	var divBody 	= 	Object;
	var divSearch 	=   getElementById('divSearch',Object,'div');
	//while (Object.id!='divSearch'){Object = Object.parentNode;}
	divs  	= divSearch.getElementsByTagName('div');
	for (var i=0;i<divs.length;i++){
		selects = divs[i].getElementsByTagName('select');
		inputs  = divs[i].getElementsByTagName('input');
		if (selects.length>=2){
			if (selects[0].value=="") continue;
			rpta+=selects[0].value+"*"+selects[1].value+"*";
			rpta+=inputs[0].value+"*";
			if (selects.length==3) rpta+=selects[2].value;
			rpta+="/";
		}
	}
	return rpta;
}
//Obtener los Ids de restriccion //SOlo para el caso obras con presupuesto
function _get_Ides(Object){
	var rpta="";
	while (Object.id!='divBody'){Object = Object.parentNode;}
	var divs = getElementById('divIds',Object,'div');
	if (divs==null) return "";
	inputs  = divs.getElementsByTagName('input');
	for (var i=0;i<inputs.length;i++){
		if (inputs[i].id=="") continue;
		rpta+=inputs[i].id+"*0*"+inputs[i].value+"*/";
	}
	return rpta;
}
//Buscar 
function _find(Object){
	while (Object.id!='divBody'){Object = Object.parentNode;}
	var where = _get_Find(Object);
	var where0= _get_Ides(Object);
	var table =   getElementById('dataTable',Object, "table");
	var page = table.getAttribute("page");
	var name = table.getAttribute("name");
	Object=table;
	while (Object.id!='divShow'){Object = Object.parentNode;}
	var div = Object;
	var url = _PATH+_SYSTEM+"?name="+name+"&type=showData&where="+where+"&where0="+where0+"&page="+page;
	div.innerHTML="Cargando...";
	getPageHTML(url, div);
}

var _cRow = "#F5F5F5";
var _cRowSel = "#F2F5A9";
function _row_mouseOver(Object)	{Object.style.background=_cRowSel;}
function _row_mouseOut(Object)	{Object.style.background=_cRow;}
//Cancelar
function _clear_divData(Object, divData){
	if (divData==null) divData="divData";	
	while (Object.id!=divData){Object = Object.parentNode;}
	Object.innerHTML="";
}
function _edit_Item(Object){
	//Obtener Variables
	while (Object.tagName!='TR'){Object = Object.parentNode;}
	var id = Object.id;
	while (Object.id!='dataTable'){Object = Object.parentNode;}
	var name = Object.getAttribute('name');
	
	while (Object.id!='divBody'){Object = Object.parentNode;}
	var divData =  getElementById('divData',Object,"div");
	var url = _PATH+_SYSTEM+"?name="+name+"&type=editItem&ide="+id;
	divData.innerHTML="Cargando...";
	getPageHTML(url, divData);
}
function _supr_Item(Object){
	var op = confirm("Desea Eliminar este Registro?");
	if (op!=true) return;
	
	//Obtener Variables
	while (Object.tagName!='TR'){Object = Object.parentNode;}
	var id = Object.id;
	while (Object.id!='dataTable'){Object = Object.parentNode;}
	var name = Object.getAttribute('name');
	
	while (Object.id!='divBody'){Object = Object.parentNode;}
	var where = _get_Find(Object);
	var where0 = _get_Ides(Object);
	var table =   getElementById('dataTable',Object, "table");
	var page = table.getAttribute("page");
	var name = table.getAttribute("name");
	
	//while (Object.id!='divShow'){Object = Object.parentNode;}
	
	var divData =  getElementById('divShow',Object,"div");
	var url = _PATH+_SYSTEM+"?name="+name+"&type=suprItem&ide="+id+"&where="+where+"&where0="+where0+"&page="+page;
	divData.innerHTML="Cargando...";
	getPageHTML(url, divData);
}

function _save_item(Object){
	while (Object.id!='divData'){Object = Object.parentNode;}
	var tableSave = Object.getElementsByTagName("table");
	if (tableSave.length>=1)	tableSave = tableSave[0];
	var error="";
	var rpta="";
	var inputs = Object.getElementsByTagName("input");
	for (i=0;i<inputs.length;i++){
		if (inputs[i].id=="") continue;
		//rpta+=inputs[i].id+"="+inputs[i].value+"&";
		var isnull=inputs[i].getAttribute("isnull");
		if (isnull=="false" && inputs[i].value=="") error="123";
		rpta+=inputs[i].id+"*"+inputs[i].value+"$";
	}
	var selects = Object.getElementsByTagName("select");
	for (i=0;i<selects.length;i++){
		if (selects[i].id=="") continue;
		var isnull=selects[i].getAttribute("isnull");
		if (isnull=="false" && selects[i].value=="") error="123";
		//rpta+=selects[i].id+"="+selects[i].value+"&";
		rpta+=selects[i].id+"*"+selects[i].value+"$";
	}
	var textarea = Object.getElementsByTagName("textarea");
	for (i=0;i<textarea.length;i++){
		if (textarea[i].id=="") continue;
		var isnull=textarea[i].getAttribute("isnull");
		if (isnull=="false" && textarea[i].value=="") error="123";
		//rpta+=selects[i].id+"="+selects[i].value+"&";
		rpta+=textarea[i].id+"*"+textarea[i].value+"$";
	}
	if (error!=""){
		alert ("Error: Falta ingresar datos\n Revise los campos con *");
		return false;
	}
	Object.innerHTML="";
	
	while (Object.id!='divBody'){Object = Object.parentNode;}
	
	var where = _get_Find(Object);
	var where0= _get_Ides(Object);
	var table =   getElementById('dataTable',Object, "table");
	var page = table.getAttribute("page");
	var name = table.getAttribute("name");
	
	//while (Object.id!='divShow'){Object = Object.parentNode;}
	
	var divData =  getElementById('divShow',Object,"div");
	var url = _PATH+_SYSTEM+"?values="+rpta+"&name="+name+"&type=saveItem&where="+where+"&where0="+where0+"&page="+page+"&ides="+where0;
	if (tableSave.id!="") url+="&ide="+tableSave.id;
	divData.innerHTML="Cargando...";
	//alert (url);
	getPageHTML(url, divData);
}
function _toPage(Object, page){
	while (Object.id!='dataTable'){Object = Object.parentNode;}
	var name = Object.getAttribute('name');
	var where = _get_Find(Object);
	var where0= _get_Ides(Object);
	while (Object.id!='divShow'){Object = Object.parentNode;}
	var div = Object;
	var url = _PATH+_SYSTEM+"?name="+name+"&type=showData&where="+where+"&where0="+where0+"&page="+page;
	div.innerHTML="Cargando...";
	getPageHTML(url, div);
}
function _new_items(Object){
	//Obtener Variables
	while (Object.id!='divBody'){Object = Object.parentNode;}
	var name = Object.getAttribute("name");
	var divData =  getElementById('divData',Object);
	var url = _PATH+_SYSTEM+"?name="+name+"&type=newItem";
	divData.innerHTML="Cargando...";
	getPageHTML(url, divData);
}	
